/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;

/**
 * Screen which handles all the settings.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class SettingsScreen extends PreferenceActivity implements OnSharedPreferenceChangeListener {

  /**
   * Code to identify the response from the activity. Value doesn't matter, just
   * has to be consistent.
   */
  private static final int REQUEST_CODE = 42;

  /**
   * This preference sets the music folder. It doesn't set the preference
   * automatically the system; its title is the preference value.
   */
  private Preference musicFolderPreference;

  /**
   * The preference shared with the other activities.
   */
  private SharedPreferences settings;

  /**
   * Editor to change the shared preferences.
   */
  private SharedPreferences.Editor editor;

  /**
   * Key to obtain the head set enabled preference.
   */
  private String headsetKey;

  /**
   * Key to obtain the folder path preference.
   */
  private String folderPathKey;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = this.getSharedPreferences(MusicQueue.PREFERENCES_NAME, Activity.MODE_PRIVATE);
    editor = settings.edit();
    headsetKey = getString(R.string.headset_key);
    folderPathKey = getString(R.string.folder_path_key);
    addPreferencesFromResource(R.xml.settings);

    PreferenceManager preferenceManager = this.getPreferenceManager();

    ((CheckBoxPreference) preferenceManager.findPreference(headsetKey)).setChecked(settings
        .getBoolean(headsetKey, false));

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

    musicFolderPreference = preferenceManager.findPreference(folderPathKey);
    musicFolderPreference.setTitle(settings.getString(folderPathKey,
        getString(R.string.folder_path_default)));
    musicFolderPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {

      @Override
      public boolean onPreferenceClick(Preference preference) {
        Intent i = new Intent();
        i.setClass(SettingsScreen.this, FolderScreen.class);
        startActivityForResult(i, REQUEST_CODE);
        return true;
      }
    });
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
      if (resultCode == Activity.RESULT_OK) {
        String musicFolder = data.getStringExtra(this.getString(R.string.folder_path_key));
        musicFolderPreference.setTitle(musicFolder);
        editor.putString(folderPathKey, musicFolder);
      }
    }
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(headsetKey)) {
      editor.putBoolean(headsetKey, sharedPreferences.getBoolean(headsetKey, true));
      editor.commit();
    }
  }
}
